<?php

namespace app\common\model;

class OrderVip extends Common
{
    protected $insert = ['dateline' => SYS_TIME];
    // // 追加属性
    protected $append = [
        'ordertime',
    ];

    protected function getAlldataAttr($value, $data)
    {
        $paytype = config('PAYTYPE');
        $status = [0 => '未支付', 1 => '支付成功', 2 => '支付失败'];
        $uinfo = model('AdminUser')->field('userid,username,realname,dealerid,puserid')->where('userid', $data['userid'])->find();
        $data['realname'] = $uinfo['realname'] ?: $uinfo['username'];
        $data['dealname'] = model('AdminUser')->where('userid', $uinfo['dealerid'])->value('realname');
        $data['payname'] = isset($paytype[$data['paytype']]) ? $paytype[$data['paytype']] : '--';
        $data['statusname'] = isset($status[$data['status']]) ? $status[$data['status']] : '--';
        $data['paydate'] = $data['paytime'] ? date('Y-m-d H:i:s', $data['paytime']) : '未支付';
        $data['dateline'] = date('Y-m-d H:i:s', $data['dateline']);
        return $data;
    }

    public function pay_success($ordersn, $userid, $price, $paytype)
    {
        $info = $this->where('ordersn', $ordersn)->find();
        if ($info['status'] >= 1) {
            return false;
        }
        $uinfo = model('AdminUser')->where('userid', $info['userid'])->find();
        if ($uinfo['vip'] && $uinfo['vipendtime'] > SYS_TIME) {
            $endtime = strtotime((date('Y', $uinfo['vipendtime']) + 1) . '-' . date('m-d', $uinfo['vipendtime']) . ' 23:59:59');
        } else {
            $endtime = strtotime(date('Y-m-d', strtotime('+1 year')) . ' 23:59:59');
        }
        // 判断是否是续费会员并记录续费次数
        $vipendtime = $uinfo['vipendtime'];
        if ($vipendtime > 0) { // 续费会员
            model('AdminUser')->where('userid', $info['userid'])->update(['viprenew' => $uinfo['viprenew'] + 1]);
        }
        //
        model('AdminUser')->where('userid', $info['userid'])->update(['vipendtime' => $endtime, 'vip' => 1]);
        exec_php('/index/cron/send_coupons', ['userid' => $info['userid'], 'type' => 'vip']);
        return $this->where('ordersn', $ordersn)->update(['status' => 1, 'paytime' => SYS_TIME]);
    }

    public function pay_fail($ordersn, $userid = 0, $price, $reason = '')
    {
        $info = $this->where('ordersn', $ordersn)->find();
        if ($info['status'] > 0) {
            return false;
        }
        return $this->where('id', $info['id'])->update(['status' => 2, 'paytime' => SYS_TIME, 'reason' => $reason]);
    }

    protected function getOrdertimeAttr($value, $data)
    {
        return $data['paytime'] ? date('Y-m-d H:i:s', $data['paytime']) : date('Y-m-d H:i:s', $data['dateline']);
    }

    protected function getPaytypeAttr($value, $data)
    {
        //1=>支付宝充值，2=>微信，3=>银行卡，4=>购车线索奖励，6=>购车线索奖励
        if ($data['paytype'] == 6) {
            return '购车线索奖励';
        } else {
            return $data['paytype'] ? '微信支付' : '未支付';
        }
    }
    // protected function getNicknameAttr($value, $data) {
    // 	$info = model("Member")->get_data(['item_id' => $data['userid']]);
    // 	return $info['nickname'];
    // }
    // protected function getStatusAttr($value) {
    // 	$arr = [0 => '正在充值', 1 => '充值成功', 2 => '充值失败'];
    // 	return $arr[$value];
    // }
    // protected function getPaynameAttr($value, $data) {
    // 	return config('PAYTYPE.' . $data['paytype']);
    // }
    // protected function getDatelineAttr($value) {
    // 	return date('Y-m-d H:i:s', $value);
    // }
    // protected function getPaytimeAttr($value) {
    // 	return $value ? date('Y-m-d H:i:s', $value) : '未完成';
    // }
    public function save_datas($data, $uinfo = [])
    {
        if (isset($data['type']) && $data['type'] == 1) {
            $oldorderlist = $this->where('ordersn', 'like', 'activate%')->where('userid', $uinfo['userid'])->where('status', '0')->where('paytime', '0')->order('dateline desc')->select();
        } else {
            $oldorderlist = $this->where('ordersn', 'like', 'vip%')->where('userid', $uinfo['userid'])->where('status', '0')->where('paytime', '0')->order('dateline desc')->select();
        }
        if (!$oldorderlist) {
            return $this->save_newdata($data, $uinfo);
        } else {
            $date = date('Ymd', $oldorderlist[0]['dateline']);
            if ($date == date('Ymd')) {
                if (isset($data['type']) && $data['type'] == 1) {
                    return $oldorderlist[0];
                }
                if ($uinfo['vip'] && $uinfo['vipendtime'] > SYS_TIME) {
                    $oldorderlist[0]['vipendtime'] = (date('Y', $uinfo['vipendtime']) + 1) . '-' . date('m-d');
                } else {
                    $oldorderlist[0]['vipendtime'] = date('Y-m-d', strtotime('+1 year'));
                }
                return $oldorderlist[0];
            } else {
                return $this->save_newdata($data, $uinfo);
            }
        }
    }

    private function save_newdata($data, $uinfo = [])
    {
        $payprice = config('VIP_PRICE');
        if ($uinfo['userid'] == '18970' || $uinfo['userid'] == '13611' || $uinfo['userid'] == '22763') {
            $payprice = 0.01;
        }
        $isactivate = false;
        if (isset($data['type']) && $data['type'] == 1) { // 如果支付目的是为了激活优惠券
            $isactivate = true;
        }
        $vipmode = 'vip' . date('Ymdhis') . rand(1000, 9999);
        $activate = 'activate' . date('Ymdhis') . rand(1000, 9999);

        $insert = [
            'userid' => $uinfo['userid'],
            'ordersn' => $isactivate ? $activate : $vipmode,
            'paytype' => isset($data['paytype']) ? $data['paytype'] : 2,
            'description' => (isset($data['type']) && $data['type'] == 1) ? '激活优惠券' : (isset($uinfo['vipendtime']) && $uinfo['vipendtime'] > 0) ? '续费荣耀会员' : '购买荣耀会员',
            'ordertype' => (isset($data['type']) && $data['type'] == 1) ? $data['type'] : 0,
            'price' => $payprice,
            'payprice' => $payprice,
            'disprice' => 0,
            // 'code' => $data['code'],
        ];
        $this->save_data($insert);
        $oinsert = [
            'userid' => $uinfo['userid'],
            'ordersn' => $insert['ordersn'],
            'paytype' => $insert['paytype'],
            'description' => $insert['description'],
            'ordertype' => (isset($data['type']) && $data['type'] == 1) ? $data['type'] : 0,
            'totalprice' => $insert['payprice'],
            'm_cid' => isset($data['mcid']) ? $data['mcid'] : 0,
        ];
        model("Order")->create($oinsert);
        if (!$isactivate) {
            if ($uinfo['vip'] && $uinfo['vipendtime'] > SYS_TIME) {
                $insert['vipendtime'] = (date('Y', $uinfo['vipendtime']) + 1) . '-' . date('m-d');
            } else {
                $insert['vipendtime'] = date('Y-m-d', strtotime('+1 year'));
            }
        }
        return $insert;
    }

    /**
     * 购车线索奖励，成为续费会员
     * @param $data
     */
    public function save_order_viprenew($userid)
    {
        $ordersn = 'vip' . date('Ymdhis') . rand(1000, 9999);
        $oinsert = [
            'userid' => $userid,
            'ordersn' => $ordersn,
            'paytype' => 6,
            'status' => 1,
            'description' => '续费荣耀会员',
            'ordertype' => 0,
            'totalprice' => 0,
            'm_cid' => 0,
            'paytime' => SYS_TIME,
            'dateline' => SYS_TIME
        ];
        model("Order")->create($oinsert);
        //
        $insert = [
            'userid' => $userid,
            'ordersn' => $ordersn,
            'paytype' => 6, // 1=>支付宝充值，2=>微信，3=>银行卡，6=>购车线索奖励，7=>购买保养套餐奖励，8=>完成任务奖励会员
            'status' => 1,
            'description' => '续费荣耀会员',
            'ordertype' => 0,
            'price' => 0,
            'payprice' => 0,
            'disprice' => 0,
            'dateline' => SYS_TIME,
            'paytime' => SYS_TIME
        ];
        return $this->save_data($insert);
    }

    /**
     * 购买保养套餐奖励，成为荣耀会员
     * @param $data
     */
    public function save_order_baoyang_vip($userid, $type)
    {
        $desc = '奖励荣耀会员';
        if ($type === 7) {
            $desc = '购买保养套餐，奖励荣耀会员';
        } elseif($type === 8) {
            $desc = '完成软文任务，奖励荣耀会员';
        }
        $ordersn = 'vip' . date('Ymdhis') . rand(1000, 9999);
        $oinsert = [
            'userid' => $userid,
            'ordersn' => $ordersn,
            'paytype' => $type,
            'status' => 1,
            'description' => $desc,
            'ordertype' => 0,
            'totalprice' => 0,
            'm_cid' => 0,
            'paytime' => SYS_TIME,
            'dateline' => SYS_TIME
        ];
        model("Order")->create($oinsert);
        //
        $insert = [
            'userid' => $userid,
            'ordersn' => $ordersn,
            'paytype' => $type, // 1=>支付宝充值，2=>微信，3=>银行卡，6=>购车线索奖励，7=>购买保养套餐奖励，8=>完成任务奖励会员
            'status' => 1,
            'description' => $desc,
            'ordertype' => 0,
            'price' => 0,
            'payprice' => 0,
            'disprice' => 0,
            'dateline' => SYS_TIME,
            'paytime' => SYS_TIME
        ];
        return $this->save_data($insert);
    }
}
